<?php

namespace App\Http\Controllers\Wap;

use App\Models\AdminParam;
use App\Models\Advertisement;
use App\Models\Car;
use App\Models\Order;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use AipOcr;

class HomeController extends WapController
{
    // 微信端首页
    public function index(AdminParam $model)
    {
        $params = $model->first();
        return $this->success($params);
    }

    // 广告列表
    public function adLists()
    {
        $now_at = Carbon::now();
        $ads = Advertisement::where('start_at', '<=', $now_at)
            ->where('end_at', '>', $now_at)
            ->orderBy('weight', 'asc')
            ->get();

        return $this->success($ads);

    }

    // 图像识别
    public function textOcr(Request $request)
    {
        $image = $request->post('image');
        $APP_ID = '15267243';
        $API_KEY = 'lCgKN4qWoxM1k2VkTaSO6KYX';
        $SECRET_KEY = 'h7qVWRYidyXtp6s1QqmaQxOa2B3Ego7R';

        $client = new AipOcr($APP_ID, $API_KEY, $SECRET_KEY);

        $result = $client->basicGeneral($image);
        return $this->success($result);
    }

    // 扫描车架号
    public function scanFrameNo(Request $request, Car $model)
    {
        $request_data = $request->all();
        $frame_no = $request_data['frame_no'];
        // 不查处下架车辆
        $res_count = $model::where('frame_no', 'like', '%'.$frame_no)->where('status', 1)->count();
        if ($res_count == 0) {
            return $this->message('暂无此车架号认证信息');
        }
        if ($res_count > 1) {
            $frame_no_cars = $model::where('frame_no', 'like', '%'.$frame_no)->where('status', 1)->pluck('frame_no');
            return $this->success($frame_no_cars);
        }
        $res = $model::where('frame_no', 'like', '%'.$frame_no)->where('status', 1)->first();

        // 不通过中间件，获取用户身份
        $header = $request->header('authorization');
        $token = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header));
        $user = [];
        if (\Cache::has($token)) {
            $user = \Cache::get($token);
        }
        // 有用户身份的情况
        if ($user) {
            // 如果用户已经诊断过
            $is_diagnosis = Order::where('type', 1)->where('car_id', $res->id)->where('user_id', $user->id)->whereNotNull('paid_at')->first();
            if ($is_diagnosis) {
                return $this->success($res);
            }
            // 判断是否首次注册赠送的诊断次数还有剩余
            $user_info = User::find($user->id);
            if ($user_info->free_diagnosis > 0) {
                // 查看之后免费诊断次数减1
                DB::table('users')->where('id', $user->id)->decrement('free_diagnosis', 1);
                return $this->success($res);
            }
        }
        // 判断诊断报告费用设置为0免费查看的情况
        $basic_param = AdminParam::basicParam();
        if ($basic_param->diagnosis_price == 0) {
            return $this->success($res);
        }
        // 否则付费
        $data = [
            'message' => '付费查看',
            'data'    => [
                'car'             => $res,
                'diagnosis_price' => $basic_param->diagnosis_price
            ]
        ];
        return $this->failed($data);

    }


}
